package com.qtp.generator.utils;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.qtp.generator.views.GeneratorParam;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 代码生成工具类
 *
 * @author xiad
 * @date 2020-08-08
 */
public class CodeUtil {

    /**
     * 单表代码生成
     * @param param
     */
    public static void generator(GeneratorParam param){
        if (!StringUtils.hasLength(param.getDaoOutput())) {
            param.setDaoOutput(param.getEntityOutput());
        }
        if (!StringUtils.hasLength(param.getXmlOutput())) {
            param.setXmlOutput(param.getEntityOutput());
        }
        if (!StringUtils.hasLength(param.getDaoPackageName())) {
            param.setDaoPackageName("dao");
        }
        if (!StringUtils.hasLength(param.getEntityPackageName())) {
            param.setEntityPackageName("models");
        }


        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setAuthor(param.getAuthor());
        gc.setOutputDir(param.getEntityOutput());
        gc.setOpen(true);
        gc.setMapperName("%sDao");
        gc.setBaseResultMap(true);
        gc.setBaseColumnList(true);
        gc.setFileOverride(true);
        gc.setDateType(DateType.ONLY_DATE);
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(param.getJdbcUrl());
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername(param.getUsername());
        dsc.setPassword(param.getPassword());
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(param.getPackageName());
        pc.setEntity(param.getEntityPackageName());
        pc.setMapper(param.getDaoPackageName());
        mpg.setPackageInfo(pc);

        // 模板配置,不使用默认模板
        TemplateConfig tc = new TemplateConfig();
        tc.setController(null);
        tc.setService(null);
        tc.setServiceImpl(null);
        tc.setXml(null);
        tc.setMapper(null);
        tc.setEntity(null);
        mpg.setTemplate(tc);

        // 自定义配置，此处将mapper.xml文件路径改为根路径
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
            List<FileOutConfig> focList = new ArrayList<>();
            String xmlPath = "/templates/mapper.xml.ftl";
            String mapperPath = "/templates/mapper.java.ftl";
            String entityPath = "/templates/entity.java.ftl";

            // 自定义输入文件名称
            focList.add(new FileOutConfig(xmlPath) {
                @Override
                public String outputFile(TableInfo tableInfo) {
                    return String.format("%s/%sMapper.xml", param.getXmlOutput(), tableInfo.getEntityName());
                }
            });
            focList.add(new FileOutConfig(mapperPath) {
                @Override
                public String outputFile(TableInfo tableInfo) {
                    return String.format("%s/%sDao.java", param.getDaoOutput(), tableInfo.getEntityName());
                }
            });
            focList.add(new FileOutConfig(entityPath) {
                @Override
                public String outputFile(TableInfo tableInfo) {
                    return String.format("%s/%s.java", param.getEntityOutput(), tableInfo.getEntityName());
                }
            });
            this.setFileOutConfigList(focList);
            }
        };
        mpg.setCfg(cfg);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(false);
        strategy.setInclude(param.getTableName());
        strategy.setControllerMappingHyphenStyle(true);
        if(StringUtils.hasLength(param.getTablePrefix())) {
            strategy.setTablePrefix(param.getTablePrefix());
        }
        if (StringUtils.hasLength(param.getFieldPrefix())) {
            strategy.setFieldPrefix(param.getFieldPrefix());
        }
        mpg.setStrategy(strategy);

        // 选择 freemarker 引擎需要指定如下加，注意 pom 依赖必须有！
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();

    }

}
